﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;

namespace RM.Weighing
{
    /// <summary>
    /// 串口工具
    /// </summary>
    public static class SerialPortTools
    {
        /// <summary>
        /// 获取本机COM列表
        /// </summary>
        /// <returns>COM列表</returns>
        public static List<string> COMList() { return SerialPort.GetPortNames().ToList(); }





        /// <summary>
        /// 获取波特率枚举列表
        /// </summary>
        /// <returns>波特率枚举列表</returns>
        public static List<SerialPortBaudRateList> BaudRateEnumList()
        {
            List<SerialPortBaudRateList> list = new List<SerialPortBaudRateList>();
            foreach (var item in Enum.GetValues(typeof(SerialPortBaudRateList)))
            {
                list.Add((SerialPortBaudRateList)Enum.Parse(typeof(SerialPortBaudRateList), item.ToString()));
            }
            return list;
        }

        /// <summary>
        /// 获取波特率列表
        /// </summary>
        /// <returns>波特率列表</returns>
        public static List<string> BaudRateValueList()
        {
            List<string> list = new List<string>();
            foreach (var item in Enum.GetValues(typeof(SerialPortBaudRateList)))
            {
                list.Add(item.ToString());
            }
            return list;
        }


        /// <summary>
        /// 根据值获取数据位枚举
        /// </summary>
        /// <returns>数据位枚举</returns>
        public static List<SerialPortDataBitList> DataBitEnumList(int num)
        {
            List<SerialPortDataBitList> list = new List<SerialPortDataBitList>();
            foreach (var item in Enum.GetValues(typeof(SerialPortDataBitList)))
            {
                list.Add((SerialPortDataBitList)Enum.Parse(typeof(SerialPortDataBitList), item.ToString()));
            }
            return list;
        }

        /// <summary>
        /// 获取数据位列表
        /// </summary>
        /// <returns>数据位列表</returns>
        public static List<string> DataBitValueList()
        {
            List<string> list = new List<string>();
            foreach (var item in Enum.GetValues(typeof(SerialPortBaudRateList)))
            {
                list.Add(item.ToString());
            }
            return list;
        }
    }

    /// <summary>
    /// 串口数据位列表
    /// </summary>
    public enum SerialPortDataBitList : int
    {
        /// <summary>
        /// 5
        /// </summary>
        FiveBits = 5,

        /// <summary>
        /// 6
        /// </summary>
        SixBits = 6,

        /// <summary>
        /// 7
        /// </summary>
        SeventBits = 7,

        /// <summary>
        /// 8
        /// </summary>
        EightBits = 8
    }

    /// <summary>
    /// 串口波特率列表
    /// </summary>
    public enum SerialPortBaudRateList : int
    {
        BaudRate_75 = 75,
        BaudRate_110 = 110,
        BaudRate_150 = 150,
        BaudRate_300 = 300,
        BaudRate_600 = 600,
        BaudRate_1200 = 1200,
        BaudRate_2400 = 2400,
        BaudRate_4800 = 4800,
        BaudRate_9600 = 9600,
        BaudRate_14400 = 14400,
        BaudRate_19200 = 19200,
        BaudRate_28800 = 28800,
        BaudRate_38400 = 38400,
        BaudRate_56000 = 56000,
        BaudRate_57600 = 57600,
        BaudRate_115200 = 115200,
        BaudRate_128000 = 128000,
        BaudRate_230400 = 230400,
        BaudRate_256000 = 256000
    }
}
